package com.xz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.xz.MemberInfo;
import com.xz.R;
import com.xz.entity.Member;
import com.xz.entity.vo.MemberQuery;
import com.xz.service.MemberService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-08
 */
@RestController
@RequestMapping("/video/member")
public class MemberController {

    @Autowired
    private MemberService memberService;
   /*
     根据条件分页查询
    */
    @PostMapping("pageCondition/{current}/{limit}")
    public R pageCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) MemberQuery memberQuery) {
        QueryWrapper<Member> queryWrapper=new QueryWrapper<>();
        //创建page对象
        Page<Member> page = new Page<>(current,limit);
        /*
         得到对象
         */
        String username = memberQuery.getUsername();
        String begin = memberQuery.getBegin();
        String end=memberQuery.getEnd();
        if(!StringUtils.isEmpty(username)) queryWrapper.like("username",username);
        if(!org.springframework.util.StringUtils.isEmpty(begin)) queryWrapper.ge("gmt_create",begin);
        if(!org.springframework.util.StringUtils.isEmpty(end)) queryWrapper.le("gmt_create",end);
        //排序
        queryWrapper.orderByDesc("gmt_create");
        IPage<Member> pageInfo = memberService.page(page, queryWrapper);
        long total = pageInfo.getTotal();
        List<Member> list = pageInfo.getRecords(); //数据list集合
        return R.ok().data("total",total).data("list",list);
    }

    @DeleteMapping("{id}")
    public  R deleteMember(@PathVariable String id){
        boolean  flag= memberService.removeById(id);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("getMember/{id}")
    public MemberInfo getMember(@PathVariable String id){
        Member member=  memberService.getById(id);
        MemberInfo memberInfo=new MemberInfo();
        memberInfo.setMemberId(id);
        memberInfo.setUsername(member.getUsername());
        memberInfo.setMobile(member.getMobile());
        return memberInfo;
    }
}

